package com.webauto.selenium;

import java.util.ArrayList;
import java.util.Set;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.webauto.util.Constant;
import com.webauto.log.Log;

import com.webauto.test_case.BaseCase;

/**
 * Selenium驱动相关操作类
 * @author lkk
 *
 */
public class SeleniumDriverUtil {
	/**
	 * PS:获取驱动对象
	 * @param browser 浏览器名称
	 */
	@SuppressWarnings("deprecation")
	public static void getDriver(String browser,String headless){
		//将浏览器名称转为大写判断浏览器后为用例基类的驱动引用赋值
		switch(browser.toUpperCase()) {
			case Constant.CHROME:
				//设置谷歌浏览器路径配置
				System.setProperty(Constant.CHROMEDRIVERKEY, Constant.CHROMEDRIVERVALUE);
				//设置谷歌浏览器驱动路径配置
				System.setProperty(Constant.CHROMEPATHKEY, Constant.CHROMEPATHVALUE);
				if("yes".equals(headless)) {
					//谷歌浏览器浏览器无头模式
					ChromeOptions chromeOptions=new ChromeOptions();
			        chromeOptions.addArguments("-headless");
			        Log.info("+++谷歌浏览器设置无头模式成功+++");
			        BaseCase.driver = new ChromeDriver(chromeOptions);
				}else {
					BaseCase.driver = new ChromeDriver();
				}
				
				break;
			case Constant.FIREFOX:
				//设置火狐浏览器路径配置
				System.setProperty(Constant.FIREFOXPATHKEY, Constant.FIREFOXPATHVALUE);
				//设置火狐浏览器驱动路径配置
				System.setProperty(Constant.FIREFOXDRIVERKEY, Constant.FIREFOXDRIVERVALUE);
				//设置火狐浏览器日志往空路径打印(不打印)
				System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
				if("yes".equals(headless)) {
					//火狐浏览器浏览器无头模式
					FirefoxOptions firefoxOptions = new FirefoxOptions();
					firefoxOptions.setHeadless(true);
					Log.info("+++火狐浏览器设置无头模式成功+++");
					BaseCase.driver = new FirefoxDriver(firefoxOptions);
				}else {
					BaseCase.driver = new FirefoxDriver();
				}
				break;
			case Constant.IE:
				//设置IE浏览器驱动路径配置
				System.setProperty(Constant.IEDRIVERKEY, Constant.IEDRIVERVALUE);
				//忽略保护模式
				DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
				desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
				BaseCase.driver = new InternetExplorerDriver(desiredCapabilities);
				break;
			default:
				Log.error("-----不支持"+browser+"浏览器");
		
		}
	}
	/**
	 * PS:页面跳转到指定url
	 * @param url
	 */
	public static void go(String url) {
		try {
			BaseCase.driver.get(url);
		}catch(Exception e) {
			Log.error("+++访问地址异常,url:"+url+"+++");
		}
		
	}
	
	/**
	 * PS:获取当前页面标题
	 * @return 标题字符串
	 */
	public static String getTitle() {
		String title = null;
		try {
			title =  BaseCase.driver.getTitle();
		}catch(Exception e) {
			Log.error("+++获取页面标题异常+++");
		}
		return title;
	}
	
	/**
	 * PS:获取当前页面的url
	 * @return url字符串
	 */
	public static String getUrl() {
		String url = null;
		try {
			url =  BaseCase.driver.getCurrentUrl();
		}catch(Exception e) {
			Log.error("+++获取页面url异常+++");
		}
		return url;
	}
	/**
	 * PS:获取当前页面的句柄信息
	 * @return 句柄
	 */
	public static String getWindowHandle() {
		String windowHandle = null;
		try {
			windowHandle =  BaseCase.driver.getWindowHandle();
		}catch(Exception e) {
			Log.error("+++获取当前页面句柄异常+++");
		}
		return windowHandle;
	}
	
	/**
	 * PS:获取所有句柄信息的集合
	 * @return 句柄信息集合对象
	 */
	public static Set<String> getWindowHandles() {
		Set<String> windowHandles = null;
		try {
			windowHandles = BaseCase.driver.getWindowHandles();
		}catch(Exception e) {
			Log.error("+++获取句柄集合异常+++");
		}
		return windowHandles;
	}
	
	/**
	 * PS:根据下标索引获取指定窗口的句柄信息
	 * @return 句柄信息
	 */
	public static String getWindowHandleByIndex(int index) {
		String windowHandle = null;
		try {
			//获取所有的句柄信息
			Set<String> windowHandles = BaseCase.driver.getWindowHandles();
			//将set集合转为ArrayList通过 下标索引获取句柄
			windowHandle = new ArrayList<String>(windowHandles).get(index);
		}catch(Exception e) {
			Log.error("+++获取指定索引句柄异常,index:+"+index+"+++");
		}
		return windowHandle;
	}
	
	/**
	 * PS:获取页面源码信息
	 */
	public static String getPageSource() {
		String pageSource = null;
		try {
			pageSource = BaseCase.driver.getPageSource();
		}catch(Exception e) {
			Log.error("+++获取页面源码异常+++");
		}
		return pageSource;
	}
	
	/**
	 * PS:关闭驱动对象
	 */
	public static void quit() {
		try {
			BaseCase.driver.quit();
		}catch(Exception e) {
			Log.error("+++关闭驱动对象异常+++");
		}
	}
	
}
